package org.jeecg.modules.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.oms.entity.DesignTask;
import org.jeecg.modules.oms.entity.DesignTaskProgress;
import org.jeecg.modules.oms.mapper.DesignTaskMapper;
import org.jeecg.modules.oms.mapper.DesignTaskProgressMapper;
import org.jeecg.modules.oms.service.IDesignTaskProgressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 设计任务进度Service实现
 */
@Slf4j
@Service
public class DesignTaskProgressServiceImpl extends ServiceImpl<DesignTaskProgressMapper, DesignTaskProgress> implements IDesignTaskProgressService {
    
    @Autowired
    private DesignTaskMapper designTaskMapper;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean recordProgress(DesignTaskProgress taskProgress) {
        if (taskProgress == null) {
            throw new JeecgBootException("进度信息不能为空");
        }
        
        if (StringUtils.isBlank(taskProgress.getTaskId())) {
            throw new JeecgBootException("任务ID不能为空");
        }
        
        // 验证任务是否存在
        DesignTask task = designTaskMapper.selectById(taskProgress.getTaskId());
        if (task == null) {
            throw new JeecgBootException("未找到对应任务");
        }
        
        // 设置记录时间
        if (taskProgress.getRecordTime() == null) {
            taskProgress.setRecordTime(new Date());
        }
        
        // 设置创建时间
        taskProgress.setCreateTime(new Date());
        
        return save(taskProgress);
    }
    
    @Override
    public List<DesignTaskProgress> getProgressListByTaskId(String taskId) {
        if (StringUtils.isBlank(taskId)) {
            throw new JeecgBootException("任务ID不能为空");
        }
        
        return baseMapper.getProgressListByTaskId(taskId);
    }
    
    @Override
    public DesignTaskProgress getLatestProgressByTaskId(String taskId) {
        if (StringUtils.isBlank(taskId)) {
            throw new JeecgBootException("任务ID不能为空");
        }
        
        return baseMapper.getLatestProgressByTaskId(taskId);
    }
} 